<template>
<div style="width: 400px;text-align:center;margin: 5px auto">
  <h1 STYLE="margin-bottom: 10px;">模板表新增</h1>
  <el-form :model="ruleForm"  status-icon :rules="rules" label-width="100%;"  ref="ruleForm" class="fulltext_form">
  <el-form-item label="模板名称"  prop="templateName" >
      <el-input type="text" v-model="ruleForm.templateName" autocomplete="off"  ></el-input>
    </el-form-item>
  <el-form-item label="模板内容"  prop="templateContent" >
      <el-input type="textarea"
                 v-model="ruleForm.templateContent" autocomplete="off"  :rows="150" ></el-input>
    </el-form-item>
  <el-form-item label="是否启用"   >
    <el-switch
      v-model="ruleForm.isEnable"
      active-color="#13ce66"
      inactive-color="#ff4949"
      active-value="1"
      inactive-value="0">
    </el-switch>
  </el-form-item>

    <el-form-item label="备注" >
      <el-input type="textarea" autosize v-model="ruleForm.remark" autocomplete="off" @keyup.native="autoSubmit($event)" ></el-input>
    </el-form-item>
    <el-form-item >
      <div style="text-align:center">
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="returnList">返回</el-button>
      </div>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
import request from "../utils/request";

export default {
  name: "add",
  data(){
    const validateTemplateName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入模板名称"));
      } else {
        callback();
      }
    };
    const validateTemplateContent = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入模板内容"));
      } else {
        callback();
      }
    };

    return{
      ruleForm: {
        projectId: '',
        projectName:'',
        templateName: '',
        remark: '',
        templateContent: '',
        isEnable: '1',
      },

      rules: {
        templateName: [{ required: true,validator: validateTemplateName, trigger: "blur" }],
        templateContent: [{ required: true,validator: validateTemplateContent, trigger: "blur" }]
      }
    }

  },
  methods: {
    autoSubmit: function (ev) {
      if (ev.code === "Enter") {
        this.submitForm('ruleForm');
      }
    },
    submitForm(formName) {
      let _this = this;
      const param = {
        projectId: _this.ruleForm.projectId,
        templateName: _this.ruleForm.templateName,
        remark: _this.ruleForm.remark,
        templateContent: _this.ruleForm.templateContent,
        isEnable: _this.ruleForm.isEnable
      };
      this.$refs[formName].validate(valid => {
        if (valid) {
          request({
            method: "POST",
            url: "/api/Template/insert",
            data: param
          })
            .then(res => {
              if (res.data.status == "1") {
                this.$message(res.data.message);
                _this.$router.push({path: "/template/list/"+_this.ruleForm.projectId+"/"+this.ruleForm.projectName});
              } else {
                this.$alert(res.data.message, {
                  confirmButtonText: '确定',
                  callback: action => {
                  }
                });
                return false;
              }
            })
            .catch(err => {
              debugger;
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    returnList(){
      this.$router.push({path: "/template/list/"+this.ruleForm.projectId+"/"+this.ruleForm.projectName});
    }
  },
  mounted() {
    this.ruleForm.projectId = this.$route.params.projectId;
    this.ruleForm.projectName = this.$route.params.projectName;
  }
}
</script>

<style scoped>

</style>
